/*******************************************************************************
 * Copyright (c) 2011 Olexandr V. Shaposhnikov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Olexandr V. Shaposhnikov - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.funkyroach.use.core.ast.expr;

import static com.funkyroach.use.core.ast.IUSASTConstants.US_AST_ASSING_OPERATOR;
import static com.funkyroach.use.core.ast.IUSASTConstants.US_AST_BUKSASSING_OPERATOR;
import static com.funkyroach.use.core.ast.IUSASTConstants.US_AST_CONCATASSING_OPERATOR;
import static com.funkyroach.use.core.ast.IUSASTConstants.US_AST_DIVASSING_OPERATOR;
import static com.funkyroach.use.core.ast.IUSASTConstants.US_AST_DIV_OPERATOR;
import static com.funkyroach.use.core.ast.IUSASTConstants.US_AST_MINUSASSING_OPERATOR;
import static com.funkyroach.use.core.ast.IUSASTConstants.US_AST_MULASSING_OPERATOR;
import static com.funkyroach.use.core.ast.IUSASTConstants.US_AST_MUL_OPERATOR;
import static com.funkyroach.use.core.ast.IUSASTConstants.US_AST_PLUSASSING_OPERATOR;

import org.eclipse.dltk.ast.ASTVisitor;
import org.eclipse.dltk.ast.expressions.Expression;
import org.eclipse.dltk.utils.CorePrinter;

public class USBinaryExpression extends USExpression {
	private Expression leftOperand;
	private Expression rightOperand;
	private int kind;

	public USBinaryExpression(Expression leftOperand, int kind,
			Expression rightOperand) {
		super();
		this.leftOperand = leftOperand;
		this.kind = kind;
		this.rightOperand = rightOperand;
	}

	public Expression getLeftOperand() {
		return leftOperand;
	}

	public void setLeftOperand(USExpression leftOperand) {
		this.leftOperand = leftOperand;
	}

	public Expression getRightOperand() {
		return rightOperand;
	}

	public void setRightOperand(USExpression rightOperand) {
		this.rightOperand = rightOperand;
	}

	public String getOperator() {
		switch (this.getKind()) {
		case US_AST_ASSING_OPERATOR:
			return "=";
		case US_AST_MULASSING_OPERATOR:
			return "*=";
		case US_AST_DIVASSING_OPERATOR:
			return "/=";
		case US_AST_PLUSASSING_OPERATOR:
			return "+=";
		case US_AST_MINUSASSING_OPERATOR:
			return "-=";
		case US_AST_CONCATASSING_OPERATOR:
			return "@=";
		case US_AST_BUKSASSING_OPERATOR:
			return "$=";
		case US_AST_MUL_OPERATOR:
			return "*";
		case US_AST_DIV_OPERATOR:
			return "/";
		}
		return "*";
	}

	public void setKind(int kind) {
		this.kind = kind;
	}

	@Override
	public int getKind() {
		return kind;
	}

	public void traverse(ASTVisitor visitor) throws Exception {
		if (visitor.visit(this)) {
			if (leftOperand != null) {
				leftOperand.traverse(visitor);
			}
			if (rightOperand != null) {
				rightOperand.traverse(visitor);
			}
			visitor.endvisit(this);
		}
	}

	public void printNode(CorePrinter output) {
		output.formatPrintLn(toString());
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(leftOperand);
		sb.append(getOperator());
		sb.append(rightOperand);
		return sb.toString();
	}
}
